//
//  ViewController.m
//  AppleWalletDemo
//
//  Created by mac on 2019/3/25.
//  Copyright © 2019年 mac. All rights reserved.
//

#import "ViewController.h"
#import <PassKit/PassKit.h>


@interface ViewController ()<PKAddPassesViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    PKAddPassButton *walletBtn = [[PKAddPassButton alloc]initWithAddPassButtonStyle:PKAddPassButtonStyleBlack];
    walletBtn.frame = CGRectMake(60, 100, self.view.frame.size.width-120, 50);
    [walletBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    walletBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [walletBtn addTarget:self action:@selector(walletAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:walletBtn];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark -- Action
- (void)walletAction:(PKAddPassButton *)sender{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"pass.json" ofType:nil];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableLeaves error:nil];
    NSString *newPath = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] bundlePath],@"/lot.plist" ];
    [array writeToFile:newPath atomically:YES];

    NSLog(@"arr==%@",array);
////    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
//
//    NSData *passData = [[NSData alloc] initWithContentsOfFile:path];
//    NSError *error = nil;
//    PKPass *pass = [[PKPass alloc] initWithData:passData error:&error];
//    if (error) {
//        NSLog(@"创建pass 过程中发生错误,错误信息:%@", error.localizedDescription);
//    }
//    PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPass:pass];
//    vc.delegate = self;
//
//    [self presentViewController:vc animated:YES completion:nil];
    
    NSString *passPath = [[NSBundle mainBundle] pathForResource:@"Lollipop" ofType:@"pkpass"];
    NSData *passData = [[NSData alloc] initWithContentsOfFile:passPath];
    NSError *error = nil;
    PKPass *pass = [[PKPass alloc] initWithData:passData error:&error];
    if (error) {
        NSLog(@"创建pass 过程中发生错误,错误信息:%@", error.localizedDescription);
    }

    PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPass:pass];
    vc.delegate = self;

    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark -- PKAddPassesDelegate
- (void)addPassesViewControllerDidFinish:(PKAddPassesViewController *)controller{
    NSLog(@"添加卡劵包完成");
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
